package com.isale.common.utils;

import com.isale.common.model.view.ComboBox;
import org.apache.commons.lang.time.DateFormatUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by bain.wang on 2016/1/14.
 */
public class YearUtil {

    public static List<ComboBox> getNianduList() {
        List<Integer> lst = getYearBefore(-1, 20);
        List<ComboBox> comboBoxList = new ArrayList<ComboBox>();
        for (Integer tmp : lst) {
            comboBoxList.add(new ComboBox(String.valueOf(tmp), String.valueOf(tmp)));
        }
        return comboBoxList;
    }

    public static List<ComboBox> getYueduList() {

        List<ComboBox> comboBoxList = new ArrayList<ComboBox>();
        for (int i = 1; i <= 12; i++) {
            comboBoxList.add(new ComboBox(String.valueOf(i), String.valueOf(i)));
        }
        return comboBoxList;
    }

    /**
     * 获取前N年
     *
     * @param current
     * @param limit
     * @return
     */
    public static List<Integer> getYearBefore(int current, int limit) {
        if (current == -1) {
            current = getCurrentNiandu();
        }

        List<Integer> rst = new ArrayList<Integer>();

        for (int i = 0; i < limit; i++) {
            rst.add(current - i);
        }

        return rst;
    }

    public static Integer getCurrentNiandu() {
        Date now = new Date();
        String yyyy = DateFormatUtils.format(now, "yyyy");
        int current = Integer.parseInt(yyyy);
        return current - 1;
    }

}
